package com.egao.orderB.service.impl;

import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.StringUtil;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.orderB.biz.BillBiz;
import com.egao.orderB.biz.BillFeeBiz;
import com.egao.orderB.biz.BillProcessBiz;
import com.egao.orderB.biz.FeeBillBiz;
import com.egao.orderB.entity.*;
import com.egao.orderB.service.FeeBillService;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//import com.egao.orderB.mapper.FeeBillMapper;
//import javax.transaction.Transactional;

/**
 * 收款单服务实现类
 * @author : Doman
 * @date : 2020-08-17 10:08:53
 * @version ：1.0
 */
@Service
public class FeeBillServiceImpl /*extends ServiceImpl<FeeBillMapper, FeeBill> */implements FeeBillService {

	@Autowired
	private FeeBillBiz feeBillBiz;
	@Autowired
	private BillBiz billBiz;
	@Autowired
	private BillProcessBiz billProcessBiz;
	@Autowired
	private BillFeeBiz billFeeBiz;

    /**
    @Override
    public PageResult<FeeBill> listPage(PageParam<FeeBill> page) {
        List<FeeBill> records = baseMapper.listPage(page);
        return new PageResult<>(records, page.getTotal());
    }

    @Override
    public List<FeeBill> listAll(Map<String, Object> page) {
        return baseMapper.listAll(page);
    } */

    @Override
    public JsonResult list(PageResult2<T> page, FeeBill feeBill, User loginUser)  throws Exception{
        try {
            JsonResult jr = feeBillBiz.list(page,feeBill,loginUser);
            List<FeeBill> feeBillList = (List<FeeBill>)jr.getData();
            if(feeBillList != null && feeBillList.size() > 0) {
                for(int i = 0; i < feeBillList.size(); i ++) {
                    FeeBill a2 = feeBillList.get(i);
                    Bill b = null;
                    String[] ids = a2.getBillIds().split(",");
                    List<Bill> billList = new ArrayList<Bill>();
                    for(String id: ids) {
                        if(Utils.isNotEmpty(id)){
                            b = new Bill();
                            b.setId(id);
                            billList.add(billBiz.getById(b));
                        }
                    }
                    if(billList != null && billList.size() > 0) {
                        a2.setBillList(billList);
                    }
                }
            }
            return jr;
        } catch (Exception e) {
            throw new Exception("查询错误,请联系平台管理员!");
        }
    }

    @Override
    public JsonResult list(FeeBill feeBill, User loginUser) {
        return JsonResult.ok().setData(feeBillBiz.list(feeBill,loginUser));
    }

    @Override
    public JsonResult getById(FeeBill feeBill) {
        return JsonResult.ok().setData(feeBillBiz.getById(feeBill));
    }

    @Transactional
    @Override
    public JsonResult save(FeeBill feeBill, User loginUser) {
        if(StringUtil.isBlank(feeBill.getBankReceiptPic(),feeBill.getBillIds()
                ,feeBill.getCustomerId(),feeBill.getCustomerName())
            && (Utils.isNotEmpty(feeBill.getStateS()) || Utils.isNotEmpty(feeBill.getStateF()))
        ){
            return JsonResult.error("请填写完整信息,操作失败!");
        }
        String[] ids = feeBill.getBillIds().split(",");
        boolean bpSRet = false;
        boolean bpFRet = false;
        for (String id : ids) {
            if (Utils.isNotEmpty(id)) {
                Bill bill = new Bill();
                bill.setId(id);
                Bill bill2 = null;
                try {
                    bill2 = billBiz.getById(bill);
                } catch (EmptyResultDataAccessException e) {
                    return JsonResult.error("订单不存在,操作失败!");
                }
                BillProcess bp = new BillProcess();
                bp.setBillId(id);
                List<BillProcess> billProcessList = billProcessBiz.list(bp);
                bpSRet = false;
                bpFRet = false;
                if(billProcessList !=null && billProcessList.size() > 0) {
                    for(BillProcess bpr: billProcessList) {
                        if(bpr.getState().equals("货代费用已审核")) {
                            bpSRet = true;
                        }
                        if(bpr.getState().equals("司机费用已审核")) {
                            bpFRet = true;
                        }
                    }
                } else {
                    return JsonResult.error("订单"+bill2.getBillOfLading()+" 费用未审核,操作失败!");
                }
                if(feeBill.getStateS().equals("已收款")||feeBill.getStateS().equals("已实收")) {
                    if (bill2.getFeeStateS()!=null && bill2.getFeeStateS().equals(feeBill.getStateS())) {
                        return JsonResult.error("订单"+bill2.getBillOfLading()+" 费用状态为:"+feeBill.getStateS()+",操作失败!");
                    }
                    if(!bpSRet) {
                        return JsonResult.error("订单"+bill2.getBillOfLading()+" 货代费用未审核,操作失败!");
                    }
                } else {
                    if (bill2.getFeeStateF()!=null && bill2.getFeeStateF().equals(feeBill.getStateF())) {
                        return JsonResult.error("订单"+bill2.getBillOfLading()+" 费用状态为:"+feeBill.getStateF()+",操作失败!");
                    }
                    if(!bpFRet) {
                        return JsonResult.error("订单"+bill2.getBillOfLading()+" 司机费用未审核,操作失败!");
                    }
                }
            }
        }
        if (feeBillBiz.save(feeBill, loginUser)) {
            for (String id : ids) {
                if (Utils.isNotEmpty(id)) {
                    Bill bill = new Bill();
                    bill.setId(id);
                    if(Utils.isNotEmpty(feeBill.getStateS())) {
                        bill.setFeeStateS(feeBill.getStateS());
                        billBiz.feeStateS(bill, loginUser);
                    } else {
                        bill.setFeeStateF(feeBill.getStateF());
                        billBiz.feeStateF(bill, loginUser);
                    }

//                    BillFee bf = new BillFee();
//                    bf.setBillId(id);
//                    bf.setFeeState("已确认");
//                    billFeeBiz.updateFeeStateByBill(bf, loginUser);
                }
            }
            return JsonResult.ok("添加成功");
        }
        return JsonResult.error("添加失败");
    }

    @Transactional
    @Override
    public JsonResult update(FeeBill feeBill, User loginUser) throws Exception {
        if(StringUtil.isBlank(feeBill.getId(),feeBill.getBankReceiptPic(),feeBill.getBillIds())
                && (Utils.isNotEmpty(feeBill.getStateS()) || Utils.isNotEmpty(feeBill.getStateF()))
        ){
            return JsonResult.error("请填写完整信息,操作失败!");
        }
        String[] ids = feeBill.getBillIds().split(",");
        boolean bpSRet = false;
        boolean bpFRet = false;
        Bill bill2 = null;
        for (String id : ids) {
            if (Utils.isNotEmpty(id)) {
                Bill bill = new Bill();
                bill.setId(id);
                try {
                    bill2 = billBiz.getById(bill);
                } catch (EmptyResultDataAccessException e) {
                    return JsonResult.error("订单不存在,操作失败!");
                }
                BillProcess bp = new BillProcess();
                bp.setBillId(id);
                List<BillProcess> billProcessList = billProcessBiz.list(bp);
                bpSRet = false;
                bpFRet = false;
                if(billProcessList !=null && billProcessList.size() > 0) {
                    for(BillProcess bpr: billProcessList) {
                        if(bpr.getState().equals("货代费用已审核")) {
                            bpSRet = true;
                        }
                        if(bpr.getState().equals("司机费用已审核")) {
                            bpFRet = true;
                        }
                    }
                } else {
                    return JsonResult.error("订单"+bill2.getBillOfLading()+" 费用未审核,操作失败!");
                }
                if(Utils.isNotEmpty(feeBill.getStateS())) {
                    if(!bpSRet) {
                        return JsonResult.error("订单"+bill2.getBillOfLading()+" 货代费用未审核,操作失败!");
                    }
                } else {
                    if(!bpFRet) {
                        return JsonResult.error("订单"+bill2.getBillOfLading()+" 司机费用未审核,操作失败!");
                    }
                }
            }
        }
        String stateS = "";
        String stateF = "";
        Bill bill = new Bill();
        if (feeBill.getStateS().equals("已实收")) {
            stateS = "已收款";
        } else if (feeBill.getStateF().equals("已实付")) {
            stateF = "已付款";
        }
        FeeBill feeBill2 = feeBillBiz.getById(feeBill);
        if(Utils.isNotEmpty(feeBill.getStateS())) {
            for (String id : feeBill2.getBillIds().split(",")) {
                bill.setFeeStateS(stateS);
                bill.setId(id);
                billBiz.feeStateS(bill,loginUser);
            }
        } else {
            for (String id : feeBill2.getBillIds().split(",")) {
                bill.setFeeStateF(stateF);
                bill.setId(id);
                billBiz.feeStateF(bill,loginUser);
            }
        }

        for (String id : ids) {
            if (Utils.isNotEmpty(id)) {
                bill = new Bill();
                bill.setId(id);
                bill2 = null;
                try {
                    bill2 = billBiz.getById(bill);
                } catch (EmptyResultDataAccessException e) {
                    throw new Exception("订单不存在,操作失败!");
                }
                BillProcess bp = new BillProcess();
                bp.setBillId(id);
                List<BillProcess> billProcessList = billProcessBiz.list(bp);
                bpSRet = false;
                bpFRet = false;
                if(billProcessList !=null && billProcessList.size() > 0) {
                    for(BillProcess bpr: billProcessList) {
                        if(bpr.getState().equals("货代费用已审核")) {
                            bpSRet = true;
                        }
                        if(bpr.getState().equals("司机费用已审核")) {
                            bpFRet = true;
                        }
                    }
                } else {
                    throw new Exception("订单"+bill2.getBillOfLading()+" 费用未审核,操作失败!");
                }
                if(Utils.isNotEmpty(feeBill.getStateS())) {
                    if (bill2.getFeeStateS()!=null && bill2.getFeeStateS().equals(feeBill.getStateS())) {
                        throw new Exception("订单"+bill2.getBillOfLading()+" "+feeBill.getStateS()+",操作失败!");
                    }
                    if(!bpSRet) {
                        throw new Exception("订单"+bill2.getBillOfLading()+" 货代费用未审核,操作失败!");
                    }
                } else {
                    if (bill2.getFeeStateF()!=null && bill2.getFeeStateF().equals(feeBill.getStateF())) {
                        throw new Exception("订单"+bill2.getBillOfLading()+" "+feeBill.getStateF()+",操作失败!");
                    }
                    if(!bpFRet) {
                        throw new Exception("订单"+bill2.getBillOfLading()+" 司机费用未审核,操作失败!");
                    }
                }
            }
        }

        if (feeBillBiz.update(feeBill, loginUser)) {
            if(Utils.isNotEmpty(feeBill.getStateS())) {
                for (String id : ids) {
                    bill = new Bill();
                    bill.setFeeStateS(feeBill.getStateS());
                    bill.setId(id);
                    billBiz.feeStateS(bill,loginUser);
                }
            } else {
                for (String id : ids) {
                    bill = new Bill();
                    bill.setFeeStateF(feeBill.getStateF());
                    bill.setId(id);
                    billBiz.feeStateF(bill,loginUser);
                }
            }
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult updateStateS(FeeBill feeBill, User loginUser) {
        if(StringUtil.isBlank(feeBill.getId(),feeBill.getStateS())){
            return JsonResult.error("请填写完整信息,操作失败!");
        }
        if (feeBillBiz.updateStateS(feeBill, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult updateStateF(FeeBill feeBill, User loginUser) {
        if(StringUtil.isBlank(feeBill.getId(),feeBill.getStateF())){
            return JsonResult.error("请填写完整信息,操作失败!");
        }
        if (feeBillBiz.updateStateF(feeBill, loginUser)) {
            return JsonResult.ok("修改成功");
        }
        return JsonResult.error("修改失败");
    }

    @Override
    public JsonResult remove(FeeBill feeBill, User loginUser) {
        if (feeBillBiz.remove(feeBill, loginUser)) {
            return JsonResult.ok("删除成功");
        }
        return JsonResult.error("删除失败");
    }

}
